// Component/articalCard/articalCard.js
Component({
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    userMessage: {
      type: Object,
      default: {
        userId: "",
        userAvatar: "",
        userNickname: "",
        userGender: "",
        userMotto: ""
      }
    },
    articleId: {
      type: String,
      default: ""
    },
    articleContent: {
      type: String,
      default: ""
    },
    createTime: {
      type: String,
      default: ""
    },
    likes: {
      type: Number,
      default: 0
    },
    // liked: {
    //   type: Boolean,
    //   default: false
    // },
    comment: {
      type: Number,
      default: 0
    },
    view: {
      type: Number,
      default: 0
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    likeSrc: "/image/like.svg",
    likedSrc: "/image/liked.svg",
    liked: false
  },
  lifetimes: {
    ready: function () {
      let articleId = Number.parseInt(this.data.articleId)
      this.setData({
        "liked": getApp().likeSet?.has(articleId)
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //查看用户信息
    check_user_message(e) {
      this.triggerEvent('check_user_message', e);
    },
    message_detail() {
      wx.navigateTo({
        url: '/pages/message_detail/message_detail?article_id=' + this.data.articleId,
      });
    },
    toggleLike() {
      let likes = this.data.likes + (this.data.liked ? -1 : 1);
      this.setData({
        "liked": !this.data.liked,
        "likes": likes >= 0 ? likes : 0
      })
      let { likeSet } = getApp();
      let articleId = Number.parseInt(this.data.articleId);
      if (likeSet.has(articleId)) likeSet.delete(articleId);
      else likeSet.add(articleId);
      getApp().requestWithToken.get('/likeArticle/' + articleId);
    }
  }
})
